package com.example.exam.sysRole.dao;

import com.alibaba.druid.util.StringUtils;
import com.example.exam.db.BaseDao;
import com.example.exam.db.PageInfoBean;
import com.example.exam.sysRole.model.SysRoleBean;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SysRoleDao extends BaseDao<SysRoleBean, Integer> {
    @Override
        /**
            * @Description:
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String findByIdSql() {
        return "select * from sys_role where id = ?";
    }
    @Override
        /**
            * @Description:
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getAddSql(){
        return "insert into sys_role (id, name, title, descr, status, create_time, create_user, type) values (:id, :name, :title, :descr, :status, :createTime, :createUser, :type) ";
    }
    @Override
        /**
            * @Description:
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getRemoveByIdSql() {
        return "update sys_role set status = -1 where id = ? ";
    }
    /**
     * @Description:
     * @Param: []
     * @return: java.util.List
     * @Author: xhw
     * @Date: 2021/7/26
     */ public List selectRole(){
        String sql="SELECT * FROM sys_role WHERE STATUS=1";
        List<SysRoleBean> list=getJtl().query(sql,new BeanPropertyRowMapper<SysRoleBean>(SysRoleBean.class));
        return list;
    }
    @Override
        /**
            * @Description:
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getUpdateAllFieldSql(){
        return "update sys_role set name=:name, title=:title, descr=:descr, status=:status where id=:id ";
    } /**
     * @Description:
     * @Param: [pageInfo, role_name, roleId]
     * @return: com.example.exam.db.PageInfoBean
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public PageInfoBean queryRoleList(PageInfoBean pageInfo, String role_name,Integer roleId){
        try {
            Map<String, Object> map = new HashMap<>();
            String sql = "SELECT * FROM sys_role WHERE STATUS =1";
            if(!StringUtils.isEmpty(role_name)) {
                sql += " and title like concat('%',:title,'%') ";
                map.put("title", role_name);
                //System.out.println(JSON.toJSONString(pageForMap(sql, map, pageInfo)));
            }
            if(roleId!=1){
                sql +=" AND id NOT IN(1)";
            }
            return this.pageForMap(sql, map, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }/**
     * @Description:
     * @Param: []
     * @return: java.util.List<com.example.exam.sysRole.model.SysRoleBean>
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public List<SysRoleBean> getRoleList(){
        String sql = "select * from sys_role where status=1";
        return listForBean(sql, null);
    }
}
